package com.supcon.exchangedata.repository.master;

import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.GasDailyOpertionInfo;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersMonth;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/03/18/9:36
 * @Description:
 */
public interface MasterStatistOtherParametersMonthRepository extends JpaRepository<StatistOtherParametersMonth, Long> {

    List<StatistOtherParametersMonth> findAllByMonthKey(String monthKey);

    StatistOtherParametersMonth findByObjIdAndTypeAndMonthKey(Long obId, Long type, String monthKey);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query("update StatistOtherParametersMonth s set s.inAvgValue = ?1 where s.objId =?2 and s.monthKey =?3 and s.type = ?4")
    public Integer updateMonth(Double currentValue, Long objId, String monthKey, Long type);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true, value = "insert into STATIST_OTHER_PARAMETERS_MONTH(ID,OBJ_ID,TYPE,MONTH_KEY,IN_AVG_VALUE,OBJ_TYPE,LAST_TIME) values(?1,?2,?3,?4,?5,?6,?7)")
    Integer insertMonth(Long id, Long objId, Long type, String monthKey, Double InAvgValue, Long objType, LocalDateTime lastTime);

    StatistOtherParametersMonth findTopByObjIdAndTypeOrderByMonthKeyDesc(Long objId,Long type);

    /**
     * 获取全部用水量
     * @return
     */
    @Transactional
    @Query(nativeQuery = true, value = "SELECT * FROM STATIST_OTHER_PARAMETERS_MONTH WHERE TYPE = ?1 and obj_id = 3 and MONTH_KEY is not null and LAST_TIME is not null")
    List<StatistOtherParametersMonth> findAllByType(String type);

    @Transactional
    @Query(nativeQuery = true, value = "SELECT * FROM STATIST_OTHER_PARAMETERS_MONTH WHERE TYPE = ?1 and obj_id = 3 and MONTH_KEY =?2 and LAST_TIME is not null")
    StatistOtherParametersMonth one(Long type,String day);
}
